﻿var SavedFeeds = {
	maxId: 0,
    tableName: "savedFeeds",
    createTable: function createTable() {
        // create SavedFeeds table
        var createStatement = ["CREATE TABLE IF NOT EXISTS ", 
                               SavedFeeds.tableName, " ",
                              "(id INTEGER PRIMARY KEY, ",
                              "guid TEXT, title TEXT, description TEXT,",
                              "content TEXT, author TEXT, link TEXT)"];
        Database.executeSql(createStatement.join(""), [], null);
    },
    init: function init() {
        if (Database.isInitialized()) {
        	SavedFeeds.createTable();
        }
    },
    insert: function insert(newItem, callback, errorCallback) {
        // add new feed to DB
    	var jsonItem = SavedFeeds._convertFromXML(newItem);
    	
    	if (jsonItem.id > 0) {
    		SavedFeeds.maxId = Math.max(SavedFeeds.maxId, jsonItem.id * 1);
    	}
    	
    	if (jsonItem.id === 0) {
    		SavedFeeds.maxId++;
    		jsonItem.id = SavedFeeds.maxId;
    	}
    	
        Database.insertOrUpdate(SavedFeeds.tableName, jsonItem, function (transaction, results) {
            if (typeof (callback) === "function") {
            	callback(transaction, results);
            }
        }, function (transaction, error) {
        	if (typeof(errorCallback) === "function") {
        		errorCallback(transaction, error);
        	}
        });
    },
    get: function get(callback, errorCallback) {
    	var sqlStatement = "SELECT * FROM " + SavedFeeds.tableName;
    	
    	Database.executeSql(sqlStatement, [], function (transaction, results) {
    		if (typeof (callback) === "function") {
            	callback(transaction, results);
            }
    	}, function (transacriom, error) {
    		if (typeof (errorCallback) === "function") {
    			errorCallback(transaction, error);
            }
    	});
    },
    getById: function getById(id, callback) {
    	var sqlStatement = "SELECT * FROM " + SavedFeeds.tableName + " as f WHERE f.id=?";
    	
    	Database.executeSql(sqlStatement, [id], function (transaction, results) {
    		if (typeof (callback) === "function") {
            	callback(transaction, results);
            }
    	});
    },
    getByGuid: function getByGuid(guid, callback) {
    	var sqlStatement = "SELECT * FROM " + SavedFeeds.tableName + " as f WHERE f.guid=?";
    	
    	Database.executeSql(sqlStatement, [guid], function (transaction, results) {
    		if (typeof (callback) === "function") {
            	callback(transaction, results);
            }
    	});
    },
    remove: function remove(id, callback) {
		var sqlStatement = "DELETE FROM " + SavedFeeds.tableName + " WHERE f.id=?";
    	
    	Database.executeSql(sqlStatement, [id], function (transaction, results) {
    		if (typeof (callback) === "function") {
            	callback(transaction, results);
            }
    	});
    },
    clear: function clear(callback) {
		var sqlStatement = "DELETE FROM " + SavedFeeds.tableName ;
    	
    	Database.executeSql(sqlStatement, [], function (transaction, results) {
    		if (typeof (callback) === "function") {
            	callback(transaction, results);
            }
    	});
    },
    _convertFromXML: function _convertFromXML(xmlItem) {
    	var jsonItem = {
        		guid: $(xmlItem).find("guid").text() || "",
        		title: $(xmlItem).find("title").text() || "No tilte",
        		description: $(xmlItem).find("description").text() || "",
        		content: $(xmlItem).find("content").text() || $(xmlItem).find("description").text() || "",
        		author: $(xmlItem).find("author").text() || "",
        		link: $(xmlItem).find("link").text() || "",
        		id: $(xmlItem).attr("id") || 0,
        };
    	return jsonItem;
    },
    _convertToXML: function _convertToXML(jsonItem) {
    	var xmlItem = $("<item>").attr("id", jsonItem.id);
    	var guid = $("<guid>").text(jsonItem.guid);
    	var title = $("<title>").text(jsonItem.title);
    	var description = $("<description>").text(jsonItem.description);
    	var content = $("<content>").text(jsonItem.content);
    	var link = $("<link>").text(jsonItem.text());
    	
    	xmlItem.append(title).append(description).append(content).append(link).append(guid);
    	
    	return xmlItem;
    }
};

SavedFeeds.init();